package st.business.tepth.business.user.busnumber.workholder.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.List;

import io.reactivex.annotations.Nullable;
import io.reactivex.disposables.Disposable;
import st.business.tepth.R;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.OperationBean;
import st.business.tepth.config.Url;
import st.business.tepth.manager.GlobalDataManager;
import st.business.tepth.manager.UserManager;
import st.shadow.library.core.net.callback.IError;
import st.shadow.library.core.net.callback.ISuccess;
import st.shadow.library.core.net.data.ObjConverter;
import st.shadow.library.core.net.rx.RxRestClient;

/**
 * @author zhangyu
 * @date 2019/2/18
 */
public class DialogActionChoose extends Dialog {
    private int mOldPosition = -1;
    private int mColorN = Color.parseColor("#8fa3b7");
    private int mColorP = Color.parseColor("#f1b647");
    private OperationBean mCurrentOperation;
    private Disposable mDisposable;
    private ActionAdapter mActionAdapter;

    public DialogActionChoose(Context context,
                              List<OperationBean> operationBeans,
                              final OnItemChooseListener<OperationBean> listener) {
        super(context);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        View view = LayoutInflater.from(context).
                inflate(R.layout.dialog_action_choose, null);

        ((TextView) view.findViewById(R.id.tv_choose_action))
                .setText("动作选择");
        RecyclerView recyclerView = view.findViewById(R.id.recycler_view_action);
        view.findViewById(R.id.iv_close).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        view.findViewById(R.id.tv_confirm).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onChoose(mCurrentOperation);
                }
                dismiss();
            }
        });
        final TextView tvConfirm = view.findViewById(R.id.tv_confirm);
        recyclerView.setLayoutManager(new GridLayoutManager(context, 4));
        if (operationBeans == null || operationBeans.isEmpty()) {
            mDisposable = RxRestClient.builder()
                    .url(Url.GetActionList)
                    .params("token", UserManager.getInstance().getToken())
                    .success(new ISuccess() {
                        @Override
                        public void onSuccess(String response) {
                            List<OperationBean> list =
                                    new ObjConverter<List<OperationBean>>().convert(response
                                            , new TypeReference<List<OperationBean>>() {
                                            });
                            if (list != null) {
                                GlobalDataManager.getInstance().setOperationBeans(list);
                            }
                            mActionAdapter.setNewData(list);
                        }
                    })
                    .error(new IError() {
                        @Override
                        public void onError(int code, String msg) {

                        }
                    })
                    .build()
                    .post();
        }
        recyclerView.setAdapter(mActionAdapter =
                new ActionAdapter(R.layout.item_operation, operationBeans));
        RecyclerView.ItemAnimator itemAnimator = recyclerView.getItemAnimator();
        if (itemAnimator != null) {
            itemAnimator.setChangeDuration(0);
        }
        recyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mOldPosition != position) {
                    mCurrentOperation = (OperationBean) adapter.getItem(position);
                    setEnable(tvConfirm, true);
                    OperationBean operationBeanNew = (OperationBean) adapter.getItem(position);
                    if (operationBeanNew != null) {
                        operationBeanNew.setSelected(true);
                    }
                    if (mOldPosition != -1) {
                        OperationBean operationBeanOld = (OperationBean) adapter.getItem(mOldPosition);
                        if (operationBeanOld != null) {
                            operationBeanOld.setSelected(false);
                        }
                    }
                    adapter.notifyItemChanged(position);
                    adapter.notifyItemChanged(mOldPosition);
                    mOldPosition = position;
                }
            }
        });
        setEnable(tvConfirm, false);
        setContentView(view);
    }

    @Override
    public void dismiss() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        super.dismiss();
    }

    private void setEnable(TextView tvConfirm, boolean isEnable) {
        tvConfirm.setBackgroundResource(isEnable ? R.drawable.shape_btn_operation_n :
                R.drawable.shape_btn_operation_t);
        tvConfirm.setTextColor(isEnable ? Color.parseColor("#333333") :
                Color.parseColor("#ffffff"));
        tvConfirm.setEnabled(isEnable);
    }

    private class ActionAdapter extends BaseQuickAdapter<OperationBean, BaseViewHolder> {

        ActionAdapter(int layoutResId, @Nullable List<OperationBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, OperationBean item) {
            helper.setText(R.id.tv_bus_number, item.getAction_name());
            if (item.isSelected()) {
                helper.setTextColor(R.id.tv_bus_number, mColorP);
                helper.setBackgroundRes(R.id.tv_bus_number, R.drawable.shape_item_operation_c);
            } else {
                helper.setTextColor(R.id.tv_bus_number, mColorN);
                helper.setBackgroundRes(R.id.tv_bus_number, R.drawable.shape_item_operation_n);
            }
        }
    }


}
